package com.his.struts.action;

import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.note.NoteDao;
import com.his.struts.dao.note.NoteImp;
import com.his.struts.dao.users.UsersDao;
import com.his.struts.dao.users.UsersImp;
import com.his.struts.form.CommentForm;
import com.his.struts.form.DoctorForm;
import com.his.struts.form.NoteForm;
import com.his.struts.util.PageUtil;
/**
 * 医生日志
 * @author liuyan
 *
 */
public class DocNoteAction extends DispatchAction {
	NoteDao dao = new NoteImp();

	/** 
	 * Method 医生取所有的日记
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward allNote(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forword = null;
		HttpSession session = request.getSession();
		//在session里面取医生的重要信息
		DoctorForm userinfo = (DoctorForm) session.getAttribute("docinfo");
		if(userinfo == null){
			return mapping.findForward("login");
		}
		int did = userinfo.getDocid();
		String sql = "notebook where d_id ="+did+" order by d_id desc";
		ResultSet rs = PageUtil.getPage(sql, 20, request);
		// 如果数据不为空 
		if(rs != null){
			// 封装
			List<NoteForm> list = dao.getBean(rs);
			// 数据放到request中
			request.setAttribute("shownote", list);
		}
		int i = dao.allDocNoteNum(did);
			request.setAttribute("allNum", i);
			forword = mapping.findForward("successall");
		return forword;
	}
	/** 
	 * Method 查看日记
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward showNote(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forword = null;
		String nid = request.getParameter("noteid");
		int noteid = Integer.parseInt(nid);
		//调用NoteDao类showNote（）取日记
		NoteForm nf = dao.showNote(noteid);
		if (nf != null) {
			request.setAttribute("mytestnote", nf);
			forword = mapping.findForward("successshow");
		} else {
			forword = mapping.findForward("fail");
		}
		return forword;
	}
	/** 
	 * Method 删除日记
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward delNote(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NoteDao ndao = new NoteImp();
		String nid = request.getParameter("noteid");
		int noteid = Integer.parseInt(nid);
		ActionForward forword = null;
		//保存一个日记
		boolean ok = ndao.delNote(noteid);
		if(ok){
			request.setAttribute("message", "删除成功");
			forword = mapping.findForward("successDel");
		}
		return forword;
	}
	/** 
	 * Method 编辑日记
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward editNote(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NoteForm nf = (NoteForm)form;
		ActionForward forword = null;
		if(!"".equals(nf.getNb_title())){
			
		// 得到session中的token
		String token = (String)request.getSession().getAttribute("token");
		// 得到网页中的token
		String newToken = request.getParameter("token");
		// 是否重复提交
		if(newToken != null && newToken.equals(token)){
			// 修改基本信息
			return mapping.findForward("successedit");
		}
		// 把最新token设置到session中
		request.getSession().setAttribute("token", newToken);
		
		NoteDao ndao = new NoteImp();
		UsersDao dao = new UsersImp();
		HttpSession session = request.getSession();
		String uname = (String)session.getAttribute("ysname");
		int did = dao.getDocid(uname);
		nf.setDocnb_id(did);
		//保存一个日记
		boolean ok = ndao.docEditNote(nf);
		if(ok){
			request.setAttribute("message", "删除成功");
			forword = mapping.findForward("successedit");
		}
		}else{
			request.setAttribute("nullEdit", "请输入标题！！！");
			forword = mapping.findForward("editNull");
		}
		return forword;
	}
}